<?php
require_once realpath (dirname (__FILE__) . '//..//pages.php');
require_once CLASS_FORM;
require_once CLASS_LI;
require_once INC_DEFINES;
require_once INC_FUNCTIONS;
require_once MDL_TABLES;

authorize ();

if (isset ($html)) {
    if (!is_a ($html, 'Tag')) {
        notify ('Invalid Data!');
    }
} else {
    notify ('No data to load!');
}
if (!isset ($view)) {
    $view = false;
}

$tables = getTables ();

echo doctype ();
?>
<html>
    <?php echo head (PAGE_TITLE, array (HTTP_CSS_INSERT, HTTP_CSS_MENU, HTTP_CSS_STYLE, HTTP_CSS_CALENDAR), array (HTTP_DIR_PHP_CALENDAR . '/scripts.js', HTTP_SCRIPT_VALIDATE, HTTP_SCRIPT_CALENDAR), '<script type="text/javascript">
  var formatSplitter = "-";
  var monthFormat = "mm";
  var yearFormat = "yyyy";
</script>');
    ?>
    <body>
        <h1 class="fixed"><?php
    echo $tables[$table];
    ?></h1><hr class="vertical" />
        <div id="form">
            <?php
            require_once VW_MENU;
            $html->addAttribute ('id', 'inp'); //for javascript
            $html->addAttribute ('onsubmit', 'validate(event)');
            $inner = $html->getInnerHTML ();
            if ($view) {
                foreach ($inner as $tag) {

                    if ($tag->getName () == 'button') {
                        $tag->addAttribute ('style', 'display:none;');
                    }
                }
            }
            $labels = $html->getTagsByTagName ('label');
            foreach ($labels as $label) {
                $inner = $label->getInnerHTML ();
                foreach ($inner as $tags) {
                    foreach ($tags as $tag) {
                        if (is_a ($tag, 'Tag')) {
                            switch ($tag->getName ()) {
                                case 'input':
                                    if ($tag->getAttribute ('type') == 'radio') {
                                        if ($view) {
                                            $tag->addAttribute ('onfocus', 'this.blur ();');
                                        }
                                    } else {
                                        $tag->addAttribute ('class', 'input');
                                    }
                                case 'textarea':
                                    if ($view) {
                                        $tag->addAttribute ('readonly', 'readonly');
                                    }
                                    break;
                                case 'select' :
                                    if ($view) {
                                        $tag->addAttribute ('onfocus', 'this.blur ();'); //Works only with firefox!
                                        /* $tmp = $tag->getInnerHTML ();
                                          foreach ($tmp as $value) {
                                          if (is_a ($value, 'Tag')) {
                                          if ($value->getAttribute ('selected') == 'selected') {
                                          $options[] = $value;
                                          }
                                          }
                                          }
                                          $tag->setName ('ul');
                                          $tag->emptyInnerHTML ();
                                          foreach ($options as $option) {
                                          $tag->add (new Li(null, $option->getInnerHTML()));
                                          } */
                                    }
                                    $tag->addAttribute ('class', 'input');
                                    break;
                            }
                        }
                    }
                }
            }
            echo $html->__toString ();
            ?>
        </div>
    </body>
</html>
